﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace LuZhouLaoJiao
{
    public partial class SetLocalDatabase : Form
    {
        public SetLocalDatabase()
        {
            InitializeComponent();
        }

        private void SetLocalDatabase_FormClosing(object sender, FormClosingEventArgs e)
        {
//             if (!Global.isKeyExist("AlreadySetLocalDB"))
//             {
//                 MessageBox.Show("此处信息为必填！");
//                 e.Cancel = true;
//             }

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string tbIPstr = tbIP.Text.ToString();
            string tbDBUserstr = tbDBUser.Text.ToString();
            string tbDBPwdstr = tbDBPwd.Text.ToString();
            string tbDBPwd2str = tbDBPwd2.Text.ToString();
            string tbDatabasestr = tbDatabase.Text.ToString();
            
            if (tbIPstr.Equals("") || tbDBUserstr.Equals("") || tbDBPwdstr.Equals("") || tbDatabasestr.Equals(""))
            {
                MessageBox.Show("所有信息均为必填！");
            }
            else 
            {
                if(!Global.IsIP(tbIPstr))
                {
                    MessageBox.Show("输入的不是合法的IP地址！");
                }
                else if(tbDBPwdstr != tbDBPwd2str)
                {
                    MessageBox.Show("密码输入不一致！");
                }
                else
                {
                    //第一次运行连接数据库时创建数据库和表
                    string str = "server = " + tbIPstr + "; user id = "+tbDBUserstr+"; password = " + tbDBPwdstr + ";persist security info = false";
                    MySqlConnection conn = new MySqlConnection(str);
                    MySqlCommand comm = new MySqlCommand();
                    comm.Connection = conn;
                    comm.CommandText = "create database if not exists " + tbDatabasestr + ";";
                    try
                    {
                        conn.Open();
                        int i = comm.ExecuteNonQuery();
                        str = "server = " + tbIPstr + "; user id = " + tbDBUserstr + "; password = " + tbDBPwdstr + "; database = " + tbDatabasestr + ";persist security info = false";
                        MySqlConnection tableConn = new MySqlConnection(str);
                        comm.Connection = tableConn;
                        try
                        {
                            tableConn.Open();
                            comm.CommandText = "CREATE TABLE if not exists batch_info(pici_id varchar(32) NOT NULL primary key,suandu float(5,3)," + "jianceren varchar(24),laiyuan varchar(32),riqi date,shijian time,beizhu text) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
                            comm.ExecuteNonQuery();

                            comm.CommandText = "CREATE TABLE if not exists sample(yangben_id int(10) unsigned NOT NULL  AUTO_INCREMENT primary key,yangbenhao smallint(6)," + "pici_id varchar(32),jiaochi varchar(20),suandu float(5,3),jiqi varchar(20),shijian time,laiyuan varchar(32),beizhu text) ENGINE=InnoDB AUTO_INCREMENT=411 DEFAULT CHARSET=utf8;";
                            comm.ExecuteNonQuery();

                            comm.CommandText = "CREATE TABLE if not exists user (id int(11) NOT NULL AUTO_INCREMENT primary key,name char(20),pwd char(32)," + "isadmin char(4)) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;";
                            comm.ExecuteNonQuery();
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            tableConn.Close();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }



                    Global.setRegeditValue("localIP", tbIPstr);
                    Global.setRegeditValue("localDBUser", tbDBUserstr);
                    Global.setRegeditValue("localDBPwd", tbDBPwdstr);
                    Global.setRegeditValue("localDatabase", tbDatabasestr);

                    Global.setRegeditValue("AlreadySetLocalDB", "YES");

                    this.Close();
                }
            }
            
        }   
    }
}
